package stark.app.function.ve;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.trackview.lib.video.changespeed.ChangeSpeedBean;
import com.stark.trackview.lib.video.changespeed.ChangeSpeedMode;
import com.stark.trackview.lib.video.changespeed.ChangeSpeedSection;
import com.stark.trackview.lib.video.changespeed.IChangeSpeedTrackViewListener;
import com.stark.ve.base.BaseOperationFragment;

import java.util.Arrays;
import java.util.List;

import stark.app.R;
import stark.app.databinding.FragmentSegmentChangeSpeedOperationBinding;
import stark.common.basic.utils.MediaUtil;

/**
 * Author：daizhongbin on 2025/4/15
 * Email：1027154338@qq.com
 * Desc:
 */
public class SegmentChangeSpeedOperationFragment extends BaseOperationFragment<FragmentSegmentChangeSpeedOperationBinding> {

    private static final Float[] sAddSpeedArray = {1.2f, 1.5f, 1.7f, 2.0f, 2.2f, 2.5f, 2.7f, 3.0f, 3.2f, 3.5f, 3.7f, 4.0f};
    private static final Float[] sMinusSpeedArray = {0.9f, 0.8f, 0.7f, 0.6f, 0.5f, 0.4f, 0.3f};
    private IChangeSpeedTrackViewListener trackViewListener;
    private SpeedDataAdapter mAdapter;
    private boolean isLoadedAdd = true;

    public void setTrackViewListener(IChangeSpeedTrackViewListener trackViewListener) {
        this.trackViewListener = trackViewListener;
    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_segment_change_speed_operation;
    }

    @Override
    protected void initView() {
        mDataBinding.tvAddSpeed.setOnClickListener(v -> {
            mDataBinding.trackView.addChangeSpeedSection(ChangeSpeedMode.ADD);
        });
        mDataBinding.tvMinusSpeed.setOnClickListener(v -> {
            mDataBinding.trackView.addChangeSpeedSection(ChangeSpeedMode.MINUS);
        });
        mDataBinding.tvFinish.setOnClickListener(v -> {
            mDataBinding.trackView.cancelCurSelection();
        });
        mDataBinding.tvFillRight.setOnClickListener(v -> {
            mDataBinding.trackView.makeSelectSectionFillRight();
        });
        mDataBinding.tvDel.setOnClickListener(v -> {
            mDataBinding.trackView.delSelectSection();
        });
        mDataBinding.trackView.setListener(new IChangeSpeedTrackViewListener() {
            @Override
            public void onTouchChangeSpeedSectionOrNot(@Nullable ChangeSpeedSection section) {
                if (trackViewListener != null)
                    trackViewListener.onTouchChangeSpeedSectionOrNot(section);
                if (section != null) {
                    mDataBinding.llSpeedContainer.setVisibility(View.VISIBLE);
                    mDataBinding.llAddMinusContainer.setVisibility(View.INVISIBLE);
                    mAdapter.setSelectValue(section.speed);
                    if (section.mode == ChangeSpeedMode.ADD) {
                        if (!isLoadedAdd) {
                            mAdapter.setNewInstance(Arrays.asList(sAddSpeedArray));
                            isLoadedAdd = true;
                        }
                    } else {
                        if (isLoadedAdd) {
                            mAdapter.setNewInstance(Arrays.asList(sMinusSpeedArray));
                            isLoadedAdd = false;
                        }
                    }
                } else {
                    mDataBinding.llSpeedContainer.setVisibility(View.INVISIBLE);
                    mDataBinding.llAddMinusContainer.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onTouchDown() {
                if (trackViewListener != null) trackViewListener.onTouchDown();
            }

            @Override
            public void onMoveToTime(long timeInMillis) {
                if (trackViewListener != null) trackViewListener.onMoveToTime(timeInMillis);
            }
        });
        mDataBinding.trackView.post(() -> {
            mDataBinding.trackView.setVideoInfo(mVideoPath, MediaUtil.getDuration(mVideoPath));
        });

        //rv
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mDataBinding.rvSpeed.setLayoutManager(layoutManager);
        mAdapter = new SpeedDataAdapter();
        mAdapter.setNewInstance(Arrays.asList(sAddSpeedArray));
        isLoadedAdd = true;
        mAdapter.setOnItemClickListener(this);
        mDataBinding.rvSpeed.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        mAdapter.setSelectValue(mAdapter.getItem(position).floatValue());
        mDataBinding.trackView.setCurSectionSpeed(mAdapter.getItem(position).floatValue());
    }

    public void moveTrackViewByTime(long timeInMillis) {
        if (mDataBinding != null) {
            mDataBinding.trackView.moveByTime(timeInMillis);
        }
    }

    public List<ChangeSpeedBean> getAllChangeSpeedBeanList() {
        if (mDataBinding != null) {
            return mDataBinding.trackView.getAllChangeSpeedBeanList();
        }
        return null;
    }
}
